home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / 3dview12.zip / VESA.CPP < prev    next >
C/C++ Source or Header  |  1996-05-28  |  9KB  |  316 lines

  1. #include <i86.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #include "defines.hpp"
  6.  
  7. #include "dpmi.hpp"
  8. #include "vesa.hpp"
  9.  
  10. extern "C" long  VESA_MinX = 0; 
  11. extern "C" long  VESA_MinY = 0;
  12. extern "C" long  VESA_MaxX = 0;
  13. extern "C" long  VESA_MaxY = 0;
  14. extern "C" DWORD VESA_BpL = 0;
  15. extern "C" char  VESA_BpP = 0;
  16. extern "C" DWORD VESA_OFSTable [4096] = {0};
  17. extern "C" WORD  VESA_Selector = 0;
  18. extern "C" DWORD VESA_ScreenSize_Byte = 0;
  19. extern "C" void* VESA_ScreenPtr = 0;
  20. extern "C" WORD  VESA_SecondScreen_Sel = 0;
  21. extern "C" DWORD VESA_SecondScreen_StartY = 0;
  22. extern "C" DWORD VESA_XResolution = 0;
  23. extern "C" DWORD VESA_YResolution = 0;
  24. extern "C" DWORD VESA_TableOfModes = 0;
  25.  
  26. #include "vesaproc.cpp"
  27.  
  28. struct VESA_INFO {
  29.     char VESASignature [4];
  30.     WORD VESAVersion;
  31.     DWORD OEMStringPtr;
  32.     DWORD Capabilities;
  33.     DWORD VideoModePtr;
  34.     WORD TotalMemory;
  35.     char Reserved [242];
  36. };
  37.  
  38. struct VESA_MODEINFO {
  39.     unsigned short ModeAttributes;
  40.     char WinAAttributes;
  41.     char WinBAttributes;
  42.     short WinGranularity;
  43.     short WinSize;
  44.     short WinASegment;
  45.     short WinBSegment;
  46.     void* WinFuncPtr;
  47.     unsigned short BytesPerScanLine;
  48.     short XResolution;
  49.     short YResolution;
  50.     char XCharSize;
  51.     char YCharSize;
  52.     char NumberOfPlanes;
  53.     char BitsPerPixel;
  54.     char NumberOfBanks;
  55.     char MemoryModel;
  56.     char BankSize;
  57.     char NumberOfImagePages;
  58.     char Reserved;
  59.     char RedMaskSize;
  60.     char RedFieldPosition;
  61.     char GreenMaskSize;
  62.     char GreenFieldPosition;
  63.     char BlueMaskSize;
  64.     char BlueFieldPosition;
  65.     char RsvdMaskSize;
  66.     char RsvdFieldPosition;
  67.     char DirectColorModeInfo;
  68.     void* PhysBasePtr;
  69.     long OffScreenMemOffset;
  70.     short OffScreenMemSize;
  71.     char Nothing [206];
  72. };
  73.     
  74. VESA_MODEINFO far* VESAModeInfo ;
  75. WORD VESAModeInfo_Seg;
  76. VESA_INFO far* VESAInfo;
  77. WORD VESAInfo_Seg;
  78.  
  79. WORD VESA_SetMode ( WORD Mode ) {
  80.     union REGS Regs;
  81.     struct SREGS SegRegs;
  82.     static struct DPMI_RMINTREGS IntRegs;
  83.     memset ( &SegRegs , 0 , sizeof ( SegRegs ) );
  84.     memset ( &IntRegs , 0 , sizeof ( IntRegs ) );
  85.     IntRegs.EAX = 0x00004F02;
  86.     IntRegs.EBX = Mode;
  87.     IntRegs.SS = 0;
  88.     IntRegs.SP = 0;
  89.     Regs.w.ax = 0x0300;
  90.     Regs.h.bl = 0x10;
  91.     Regs.h.bh = 0;
  92.     Regs.w.cx = 0;
  93.     SegRegs.es = FP_SEG ( &IntRegs );
  94.     Regs.x.edi = FP_OFF ( &IntRegs );
  95.     int386x ( 0x31 , &Regs , &Regs , &SegRegs );
  96.     return WORD( IntRegs.EAX );
  97. };
  98.  
  99. WORD VESA_SetScanLineLength ( WORD L ) {
  100.     union REGS Regs;
  101.     struct SREGS SegRegs;
  102.     static struct DPMI_RMINTREGS IntRegs;
  103.     memset ( &SegRegs , 0 , sizeof ( SegRegs ) );
  104.     memset ( &IntRegs , 0 , sizeof ( IntRegs ) );
  105.     IntRegs.EAX = 0x00004F06;
  106.     IntRegs.EBX = 0;
  107.     IntRegs.ECX = L;
  108.     IntRegs.SS = 0;
  109.     IntRegs.SP = 0;
  110.     Regs.w.ax = 0x0300;
  111.     Regs.h.bl = 0x10;
  112.     Regs.h.bh = 0;
  113.     Regs.w.cx = 0;
  114.     SegRegs.es = FP_SEG ( &IntRegs );
  115.     Regs.x.edi = FP_OFF ( &IntRegs );
  116.     int386x ( 0x31 , &Regs , &Regs , &SegRegs );
  117.     return WORD( IntRegs.EAX );
  118. };
  119.  
  120. /*
  121. DWORD SetDispStart;
  122.      
  123. WORD GetVESAProcs() {
  124.     union REGS Regs;
  125.     struct SREGS SegRegs;
  126.     static struct DPMI_RMINTREGS IntRegs;
  127.     memset ( &SegRegs , 0 , sizeof ( SegRegs ) );
  128.     memset ( &IntRegs , 0 , sizeof ( IntRegs ) );
  129.     IntRegs.EAX = 0x00004F0A;
  130.     IntRegs.EBX = 0;
  131.     IntRegs.SS = 0;
  132.     IntRegs.SP = 0;
  133.     Regs.w.ax = 0x0300;
  134.     Regs.h.bl = 0x10;
  135.     Regs.h.bh = 0;
  136.     Regs.w.cx = 0;
  137.     SegRegs.es = FP_SEG ( &IntRegs );
  138.     Regs.x.edi = FP_OFF ( &IntRegs );
  139.     int386x ( 0x31 , &Regs , &Regs , &SegRegs );
  140.     DWORD TableStart = (IntRegs.ES << 4) + WORD( IntRegs.EDI );
  141.     SetDispStart = TableStart + *(WORD*)(TableStart+2);
  142.     return WORD( IntRegs.EAX );
  143. };
  144. */
  145.  
  146. void VESA_AllocateModeInfo( VESA_MODEINFO far* &VMI, WORD &VMI_SEG ) {
  147.     union REGS Regs;
  148.     struct SREGS SegRegs;
  149.     memset ( &SegRegs , 0 , sizeof ( SegRegs ) );
  150.     Regs.w.ax = 0x0100;
  151.     Regs.w.bx = 0x0010;
  152.     int386x ( 0x31 , &Regs , &Regs , &SegRegs );
  153.     WORD Segment = Regs.w.ax;
  154.     WORD Selector = Regs.w.dx;
  155.     VMI = Selector:>(VESA_MODEINFO*) 0;
  156.     VMI_SEG = Segment;
  157. };
  158.      
  159. WORD VESA_GetModeInfo ( WORD Mode , WORD VMI_SEG ) {
  160.     union REGS Regs;
  161.     struct SREGS SegRegs;
  162.     static struct DPMI_RMINTREGS IntRegs;
  163.     memset ( &SegRegs , 0 , sizeof ( SegRegs ) );
  164.     memset ( &IntRegs , 0 , sizeof ( IntRegs ) );
  165.     IntRegs.EAX = 0x00004F01;
  166.     IntRegs.ECX = Mode;
  167.     IntRegs.ES = VMI_SEG;
  168.     IntRegs.EDI = 0;
  169.     IntRegs.SS = 0;
  170.     IntRegs.SP = 0;
  171.     Regs.w.ax = 0x0300;
  172.     Regs.h.bl = 0x10;
  173.     Regs.h.bh = 0;
  174.     Regs.w.cx = 0;
  175.     SegRegs.es = FP_SEG ( &IntRegs );
  176.     Regs.x.edi = FP_OFF ( &IntRegs );
  177.     int386x ( 0x31 , &Regs , &Regs , &SegRegs );
  178.     return WORD( IntRegs.EAX );    
  179. };
  180.  
  181. void VESA_AllocateInfo( VESA_INFO far* &VI, WORD &VI_SEG ) {
  182.     union REGS Regs;
  183.     struct SREGS SegRegs;
  184.     memset ( &SegRegs , 0 , sizeof ( SegRegs ) );
  185.     Regs.w.ax = 0x0100;
  186.     Regs.w.bx = 0x0010;
  187.     int386x ( 0x31 , &Regs , &Regs , &SegRegs );
  188.     WORD Segment = Regs.w.ax;
  189.     WORD Selector = Regs.w.dx;
  190.     VI = Selector:>(VESA_INFO*) 0;
  191.     VI_SEG = Segment;
  192. };
  193.  
  194. WORD VESA_GetInfo ( WORD VI_SEG ) {
  195.     union REGS Regs;
  196.     struct SREGS SegRegs;
  197.     static struct DPMI_RMINTREGS IntRegs;
  198.     memset ( &SegRegs , 0 , sizeof ( SegRegs ) );
  199.     memset ( &IntRegs , 0 , sizeof ( IntRegs ) );
  200.     IntRegs.EAX = 0x00004F00;
  201.     IntRegs.ES = VI_SEG;
  202.     IntRegs.EDI = 0;
  203.     IntRegs.SS = 0;
  204.     IntRegs.SP = 0;
  205.     Regs.w.ax = 0x0300;
  206.     Regs.h.bl = 0x10;
  207.     Regs.h.bh = 0;
  208.     Regs.w.cx = 0;
  209.     SegRegs.es = FP_SEG ( &IntRegs );
  210.     Regs.x.edi = FP_OFF ( &IntRegs );
  211.     int386x ( 0x31 , &Regs , &Regs , &SegRegs );
  212.     return WORD( IntRegs.EAX );    
  213. };
  214.  
  215. WORD VESA_CheckIfSupported ( WORD Mode , DWORD Table ) {
  216.     WORD* Modes;
  217.     Table = (DWORD)( Table & 0xffff ) +
  218.             (DWORD)( ( Table >> 16 ) << 4 );
  219.     for ( Modes = (WORD*)Table;
  220.           ( *Modes != Mode ) && ( *Modes != WORD( -1 ) );
  221.           Modes += 1 );
  222.     return *Modes;
  223. };
  224.  
  225. void VESA_PrintModeList8B() {
  226.     WORD *Modes = (WORD*)VESA_TableOfModes;
  227.     for ( ; *Modes != WORD(-1); Modes++ ) {
  228.         VESA_GetModeInfo( *Modes, VESAModeInfo_Seg );
  229.         if ( (*VESAModeInfo).BitsPerPixel == 8 )
  230.             printf( "Mode %x : %ix%i\n", *Modes, (*VESAModeInfo).XResolution,
  231.                                                  (*VESAModeInfo).YResolution );
  232.     };
  233. };
  234.  
  235. WORD VESA_InitMode ( WORD Mode ) {
  236.     WORD Status;
  237.  
  238.     WORD Supported = VESA_CheckIfSupported( Mode, (*VESAInfo).VideoModePtr );
  239.     if ( Supported == WORD(-1) ) {
  240.         printf ( "\n" );
  241.         printf ( "ERROR : VESA-Mode not supported !\n" );
  242.         return 1; };
  243.  
  244.     Status = VESA_GetModeInfo( Mode , VESAModeInfo_Seg );
  245.     if ( Status >= 0x0100 ) {
  246.         printf ( "\n" );
  247.         printf ( "ERROR : Couldn't get VESA-Modeinfo !\n");
  248.         return 1; };
  249.  
  250.     if ( (*VESAModeInfo).ModeAttributes & 0x80 == 0 ) {
  251.         printf ( "\n" );
  252.         printf ( "ERROR : VESA-Mode doesn't support Flat Memory !\n" );
  253.         return 1; };
  254.  
  255.     VESA_XResolution = (*VESAModeInfo).XResolution;
  256.     VESA_YResolution = (*VESAModeInfo).YResolution;
  257.     VESA_BpL = (*VESAModeInfo).BytesPerScanLine;
  258.     VESA_BpP = (*VESAModeInfo).BitsPerPixel;
  259.     VESA_ScreenSize_Byte = VESA_BpL * (*VESAModeInfo).YResolution;
  260.     VESA_SetClipArea( 0, 0, (*VESAModeInfo).XResolution - 1, (*VESAModeInfo).YResolution - 1 );
  261.     for ( int i=0 ; i < 4096 ; i++ ) {
  262.         VESA_OFSTable[i] = i*VESA_BpL;
  263.     };
  264.     VESA_SecondScreen_StartY = (*VESAModeInfo).YResolution;
  265.  
  266.     Status = VESA_SetMode ( WORD( Mode + 0x4000 ) );
  267.  
  268.     if ( Status >= 0x0100 ) {
  269.         VESA_SetMode ( 3 );
  270.         printf ( "\n" );
  271.         printf ( "ERROR : Couldn't initialize VESA-Mode ...\n" );
  272.         return 1; };
  273.  
  274.     // VESA_GetProcs();
  275.         
  276.     VESA_ScreenPtr = DPMI_FreeAccess ( (*VESAModeInfo).PhysBasePtr , 0x800000 );
  277.     VESA_Selector = DPMI_GetSelector ( VESA_